﻿using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.Util;
using System.ComponentModel;
using Kingdee.BOS.Orm.DataEntity;
using ConsoleApplication.WebAPI;
using Newtonsoft.Json.Linq;
using Kingdee.BOS.App.Data;

namespace NS
{
    [HotUpdate]
    [Description("新品规则表保存后新增供应商")]
    public class CreateSupplier : AbstractDynamicFormPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.Equals("tbSplitApprove")|| e.BarItemKey.Equals("tbApprove"))
            {
                string F_WNEG_Assistant1 = "", MEssage="",FNumber = "", FSUPID = "",FName = "", FSupplierId="", F_WNEG_Group1="", F_WNEG_Base3="", F_WNEG_Text2="",
                    FCountry="", FLegalPerson="", FSOCIALCRECODE="", F_WNEG_Text4="", F_WNEG_Remark="", F_WNEG_Remark12="", F_WNEG_Date="", F_WNEG_Remark13="";
                if (this.Model.GetValue("F_WNEG_Assistant1") != null)
                {
                    F_WNEG_Assistant1 = Convert.ToString((this.Model.GetValue("F_WNEG_Assistant1") as DynamicObject)["FNumber"]);
                }
                if (this.Model.GetValue("FNumber") != null)
                {
                    FNumber = Convert.ToString(this.Model.GetValue("FNumber"));
                }
                if (this.Model.GetValue("FName") != null)
                {
                    FName = Convert.ToString(this.Model.GetValue("FName"));
                }
                if (this.Model.GetValue("F_WNEG_Group1") != null)
                {
                    F_WNEG_Group1 = Convert.ToString((this.Model.GetValue("F_WNEG_Group1") as DynamicObject)["Number"]);
                }
                if (this.Model.GetValue("F_WNEG_Base3") != null)
                {
                    F_WNEG_Base3 = Convert.ToString((this.Model.GetValue("F_WNEG_Base3") as DynamicObject)["Number"]);
                }
                if (this.Model.GetValue("FSUPID") != null)
                {
                    FSUPID = Convert.ToString(this.Model.GetValue("FSUPID"));
                }
                if (this.Model.GetValue("F_WNEG_Text2") != null)
                {
                    F_WNEG_Text2 = Convert.ToString(this.Model.GetValue("F_WNEG_Text2"));
                }

                if (this.Model.GetValue("FCountry") != null)
                {
                    FCountry = Convert.ToString((this.Model.GetValue("FCountry") as DynamicObject)["FNumber"]);
                }
                if (this.Model.GetValue("FLegalPerson") != null)
                {
                    FLegalPerson = Convert.ToString(this.Model.GetValue("FLegalPerson"));
                }
                if (this.Model.GetValue("FSOCIALCRECODE") != null)
                {
                    FSOCIALCRECODE = Convert.ToString(this.Model.GetValue("FSOCIALCRECODE"));
                }
                if (this.Model.GetValue("F_WNEG_Text4") != null)
                {
                    F_WNEG_Text4 = Convert.ToString(this.Model.GetValue("F_WNEG_Text4"));
                }
                if (this.Model.GetValue("F_WNEG_Remark") != null)
                {
                    F_WNEG_Remark = Convert.ToString(this.Model.GetValue("F_WNEG_Remark"));
                }
                if (this.Model.GetValue("F_WNEG_Remark12") != null)
                {
                    F_WNEG_Remark12 = Convert.ToString(this.Model.GetValue("F_WNEG_Remark12"));
                }
                if (this.Model.GetValue("F_WNEG_Date") != null)
                {
                    F_WNEG_Date = Convert.ToString(this.Model.GetValue("F_WNEG_Date"));
                }
                if (this.Model.GetValue("F_WNEG_Remark13") != null)
                {
                    F_WNEG_Remark13 = Convert.ToString(this.Model.GetValue("F_WNEG_Remark13"));
                }
                if (!FSUPID.Equals(""))
                {
                    string SQL0 = "/*dialect*/" + string.Format(@"select FSupplierId from t_BD_Supplier where fnumber='{0}'
                    ", F_WNEG_Assistant1 + FNumber);
                    DynamicObjectCollection DynamData0 = DBUtils.ExecuteDynamicObject(this.Context, SQL0);
                    if(DynamData0.Count>0)
                    {
                        FSupplierId = Convert.ToString(DynamData0[0]["FSupplierId"]);
                        MEssage = "修改供应商成功！";
                    }
                }
                else
                {
                    FSupplierId = "0";
                    MEssage = "新增供应商成功！";
                }
                var result = InvokeHelper.Login();
                var iResult = JObject.Parse(result)["LoginResultType"].Value<int>();
                if (iResult == 1)
                {
                    JObject json = new JObject();
                    json.Add("IsDeleteEntry", "true");
                    json.Add("ValidateFlag", "true");
                    json.Add("IsAutoAdjustField", "true");
                    json.Add("IsAutoSubmitAndAudit", "true");
                    JObject item = new JObject();
                    item.Add("FSupplierId", FSupplierId);
                    JObject Obj = new JObject();
                    Obj.Add("FNumber", "100");
                    item.Add("FCreateOrgId", Obj);
                    Obj = new JObject();
                    Obj.Add("FNumber", "100");
                    item.Add("FUseOrgId", Obj);
                    Obj = new JObject();
                    Obj.Add("FNumber", F_WNEG_Group1);
                    item.Add("FGroup", Obj);
                    item.Add("FNumber", F_WNEG_Assistant1+ FNumber);
                    item.Add("FName", FName);
                    item.Add("F_WNEG_Text3", F_WNEG_Text2);
                    item.Add("F_WNEG_Text4", F_WNEG_Text4);
                    item.Add("F_WNEG_Remark", F_WNEG_Remark);
                    item.Add("F_WNEG_Remark12", F_WNEG_Remark12);
                    item.Add("F_WNEG_Date", F_WNEG_Date);
                    item.Add("F_WNEG_Remark13", F_WNEG_Remark13);
                   
                    JObject FFinanceInfo = new JObject();
                    Obj = new JObject();
                    Obj.Add("FNumber", F_WNEG_Base3);
                    FFinanceInfo.Add("FPayCondition", Obj);
                    item.Add("FFinanceInfo", FFinanceInfo);

                    JObject FBaseInfo = new JObject();
                    
                    Obj = new JObject();
                    Obj.Add("FNumber", FCountry);
                    FBaseInfo.Add("FCountry", Obj);
                    FBaseInfo.Add("FSOCIALCRECODE", FSOCIALCRECODE);
                    item.Add("FBaseInfo", FBaseInfo);

                    json.Add("Model", item);
                    result = InvokeHelper.Save("BD_Supplier", json.ToString());
                    var result0 = JObject.Parse(result);
                    var IsSuccess = result0["Result"]["ResponseStatus"]["IsSuccess"];
                    if (IsSuccess.ToString().Equals("True"))
                    {
                        var SuccessEntitysId = result0["Result"]["ResponseStatus"]["SuccessEntitys"][0]["Id"];
                        this.Model.SetValue("FSUPID", SuccessEntitysId.ToString());
                        this.View.UpdateView("FSUPID");
                        this.View.ShowMessage(MEssage);
                    }
                    else
                    {
                        var Errors= result0["Result"]["ResponseStatus"]["Errors"];
                        this.View.ShowErrMessage("新增或修改供应商失败！+'"+ Errors + "'");
                    }
                }
            }
        }
    }
}
